﻿using IBLL;
using IDAL;
using Models;

namespace BLL
{
    public class HouseNumBLL : IHouseNumBLL
    {
        /// <summary>
        /// 面向接口开始
        /// 注入的是接口
        /// </summary>
        public HouseNumBLL(IBaseDAL<HouseNum> dAL)
        {
            DAL = dAL;
        }

        public IBaseDAL<HouseNum> DAL { get; }

        public async Task<int> AddHouseNum(HouseNum house)
        {
            return await DAL.AddAsync(house);
        }

        /// <summary>
        /// 批量真删除
        /// </summary>
        /// <param name="idstring">由逗号分隔的id字符串</param>
        /// <returns></returns>
        public async Task<int> DeleteRange(string idstring)
        {
            //先查再删
            //List<int> ids = new List<int>() { 21, 123, 43, 1, 4 };
            // string[] s = idstring.Split(',');
            List<int> ids = idstring.Split(',').Select(x=> Convert.ToInt32(x)).ToList();

            var h = DAL.GetAll().Where(x=> ids.Contains(x.Id)).ToList();
            return  await DAL.DeleteRangeAsync(h);
        }

        /// <summary>
        /// 批量逻辑删除【状态修改】
        /// 本质是修改
        /// </summary>
        /// <param name="idstring"></param>
        /// <returns></returns>
        public async Task<int> DeleteRangeLogic(string idstring)
        {
            List<int> ids = idstring.Split(',').Select(x => Convert.ToInt32(x)).ToList();
            var h = DAL.GetAll().Where(x => ids.Contains(x.Id)).ToList(); //查
            foreach (var item in h)
            {
                item.State = 2;  //修改状态为已预订
            }
            return await DAL.UpateRangeAsync(h);  //保存回数据表，本质是修改
        }

        /// <summary>
        /// 空闲和已预订修改
        /// 本质是修改
        /// </summary>
        /// <param name="idstring"></param>
        /// <returns></returns>
        public async Task<int> UpdateState(string idstring)
        {
            List<int> ids = idstring.Split(',').Select(x => Convert.ToInt32(x)).ToList();
            var h = DAL.GetAll().Where(x => ids.Contains(x.Id)).ToList(); //查
            foreach (var item in h)
            {
                item.State = item.State == 1 ? 2 : 1;  //已预订的赋值为空闲，空闲的赋值为已预订，
            }
            return await DAL.UpateRangeAsync(h);  //保存回数据表，本质是修改
        }   
        public dynamic GetHouseNumPageWhere(int index, int size, decimal pmin, decimal pmax, int State)
        {
            //分页思路：预备查，拼接条件，取总数，分页查询
            var query =  DAL.GetAll();
            if (pmin > 0)
            {
                query = query.Where(x=>x.Price>=pmin);
            }
            if (pmax > 0)
            {
                query = query.Where(x => x.Price <= pmax);
            }
            if (State > 0)
            {
                query = query.Where(x => x.State == State);
            }
            var c = query.Count();
            var d= query.OrderByDescending(c=>c.Price).Skip((index-1)*size).Take(size).ToList();


            return new
            {
                a = c,
                b = d
            };
        }

    }
}
